﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using System8ToyMC.Plots;

namespace t_System8ToyMCControls
{
    [TestClass]
    public class t_HistogramData
    {
        [TestMethod]
        public void TestMethod()
        {
            ///
            /// Test default ctor
            /// 

            HistogramData h = new HistogramData();
        }

        [TestMethod]
        public void TestLargeData()
        {
            HistogramData h = new HistogramData();
            var data = GenerateData(10000, 0.0, 1.0);
            for (int i = 0; i < data.Length; i++)
            {
                h.Accumulate(data[i]);
            }

            Assert.AreEqual(10000, h.TotalCount, "Number of entries isn't right");
            Assert.IsTrue(h.Dimension >= 49, "Number of bins isn't right");
            Assert.IsTrue(h.Dimension <= 51, "Number of bins isn't right");
        }

        [TestMethod]
        public void TestFlatHisto()
        {
            HistogramData h = new HistogramData(9, 0.0, 10.0);
            for (int i = 0; i < 900; i++)
            {
                h.Accumulate(((double)i) / 10.0);
            }

            Assert.AreEqual(9, h.Dimension, "Number of bins isn't right");
            for (int i = 0; i < h.Dimension; i++)
            {
                Assert.AreEqual(100, h.YValueAt(i), "Bin contents " + i.ToString() + " isn't right");
            }
        }

        [TestMethod]
        public void TestSingleValue()
        {
            HistogramData h = new HistogramData(50);
            for (int i = 0; i < 100; i++)
            {
                h.Accumulate(3000);
            }
            Assert.AreEqual(100, h.Count, "Expected 100 items!");
            Assert.AreEqual(49, h.Dimension, "Expected 50 bins");
        }

        [TestMethod]
        public void TestSingleValueZero()
        {
            HistogramData h = new HistogramData(50);
            for (int i = 0; i < 100; i++)
            {
                h.Accumulate(0);
            }
            Assert.AreEqual(100, h.Count, "Expected 100 items!");
            Assert.AreEqual(51, h.Dimension, "Expected 50 bins");
        }

        private static double[] GenerateData(int dsize, double min, double max)
        {
            Random r = new Random();
            double[] data = new double[dsize];
            for (int i = 0; i < dsize; i++)
            {
                data[i] = r.NextDouble();
                data[i] *= (max - min);
                data[i] += min;
            }
            return data;
        }

        [TestMethod]
        public void TestNaNData()
        {
            HistogramData h = new HistogramData(50);
            for (int i = 0; i < 100; i++)
            {
                h.Accumulate(double.NaN);
            }
            Assert.AreEqual(51, h.Dimension, "Expected 51 bins");
            Assert.AreEqual(0, h.Count, "Expected no items");
        }

    }
}